<?php

namespace App\Http\Controllers\Ad;

use App\Http\Controllers\BaseController;
use App\Models\AdCategory;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Redis;

class CategoryController extends BaseController
{
    public function index()
    {

       $data =   AdCategory::query()->where('status',1)->get();


       return $this->success($data);

    }



    public function getAccessToken()
    {


        if (Cache::has('access_token')){
            return Cache::get('access_token');
        }

        $result =  Http::post('https://qw-openapi-tx.dustess.com/auth/v1/access_token/token',[
            'clientId' => 'cfk65r8h3fzgc',
            'clientSecret' => 'Lef6GJ2vA4MRDl4kF28mfoGxP6Y5nG5s'
        ])->json();

        if ($result['code']==0){
            $time = strtotime($result['data']['expiresIn'])-time();
            Cache::put('access_token',$result['data']['accessToken'],$time);

            return  $result['data']['accessToken'];
        }

        return '';
    }

    public function batchAddCustomer()
    {
        $url = 'https://qw-openapi-tx.dustess.com/customer/v1/batchAddCustomer?accessToken='.$this->getAccessToken();

        $result = Http::post($url,[
            'customer_list' => [
                [
                    'remark' => '小程序',
                    'mobiles' => [
                        [
                            'tel' =>'15202411112',
                            'type' => 'mobile',
                            'display' => '手机号'
                        ]
                    ],
                    'categoryId' => '00000000-0000-0000-0000-000000000000',
                    'pool' => '232810069270495232'
                ],
                [
                    'remark' => 'xiongxiong123',
                    'mobiles' => [
                        [
                            'tel' =>'18509212669',
                            'type' => 'mobile',
                            'display' => '手机号'
                        ]
                    ],
                    'categoryId' => '00000000-0000-0000-0000-000000000000',
                    'pool' => '232810069270495232'
                ]
            ]
        ])->json();

        dd($result);
    }

    public function create($data)
    {
       $result =  Http::withHeaders([
           'accessToken' => $this->getAccessToken()
       ])->post('https://qw-openapi-tx.dustess.com/java-data-exchange-srv/api/goods/v1/create',$data)->json();


       return $result;
    }

    public function createValet($data)
    {
        $url = 'https://qw-openapi-tx.dustess.com/java-data-exchange-srv/api/order/v1/create-valet?accessToken='.$this->getAccessToken();
        $result =  Http::post($url,[
            'orderList' =>[[
                'address' => [
                        'address' => '测试',
                        'areaName' => '东城区',
                        'cityName' => '北京市',
                        'consignee' => '张三',
                        'mobile' => '18509212669',
                        'provinceName' => '北京',
                    ],
                'creator' => '64',
                'customerId' => 'b853cad4-df57-11ee-b399-3ea1f245131e',
                'dealTime' => '2024-03-06 16:15:40',
                'depId' => '6',
                'outOrderNumber'=>'202403121655485252495748',
                'followUserId' => '64',
                'goods' => [
                    [
                        'skuId' => 'ccb42879-3b88-49d3-8bcd-a7c48d6b57f3',
                        'number' => 1,
                        'amount' => '121.00',
                    ]
                ],
                'payType' => 1,
                'status' => 20,
            ]
            ]
        ])->json();

        return $result;
    }

    public function getStaffList($param)
    {
       return Http::post('https://qw-openapi-tx.dustess.com/customer/v1/getStaffList?accessToken='.$this->getAccessToken(),$param)
           ->json();
    }
}
